// Java defined library
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;



/**
 * @author James
 * 
 */
public class RunGoogleImageQueries {

	/**
	 * @param args
	 */

	public final static int NUM_RESULTS = 10;

	public static void main(String[] args) {

		if (args.length != 1) {
			System.err
					.println("Args should have the input");
			System.exit(1);
		}

		
		try {
		File fileIn = new File(args[1]);

		// Signifies the file the urls will be written to
		File fileOut = new File("results.txt");
		
		if(!(fileOut.exists())){
			if(!(fileOut.createNewFile())){
				System.err.println("Could not open results file");
				System.exit(1);
			}	
		}
			
			
		GoogleImageEngine images = new GoogleImageEngine();

		
			String query;
			ArrayList<String> results;

			BufferedReader queryReader = new BufferedReader(new FileReader(
					fileIn));

			BufferedWriter queryWriter = new BufferedWriter(new FileWriter(
					fileOut));

			while ((query = queryReader.readLine()) != null) {
				results = images.doQuery(query, NUM_RESULTS);

				// Take the results and add them to the file
				for (String res : results) {
					queryWriter.append(res);
				}

			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
